package com.cetcs.kmga.dataManager.service.impl.dataManager;

import com.alibaba.fastjson.JSON;
import com.cetc.cloud.kmga.util.MqUtil;
import com.cetcs.kmga.dataManager.dao.mybatis.IpSettingManageMapper;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.IpSettingVo;
import com.cetcs.kmga.dataManager.entity.global.AuditIpMqMessage;
import com.cetcs.kmga.dataManager.service.dataManager.IpSettingManageService;
import com.cetcs.kmga.dataManager.util.MqParamsUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;
import jdk.nashorn.internal.ir.IdentNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by root on 2017/9/21.
 */
@Service
public class IpSettingManageServiceImpl implements IpSettingManageService {

    @Autowired
    private IpSettingManageMapper ipSettingManageMapper;

    @Override
    public int addIpSetting(IpSettingVo vo){
        if(ipSettingManageMapper.countIdByNameAndId(vo.getName(),null) > 0){
            throw new RuntimeException(vo.getName() + "已存在");
        }
        vo.setUpdateTime(new Date());
        Integer result = ipSettingManageMapper.addIpSetting(vo);
        if(result>0) {
            /**
             * yyangs mq消息 安全审计规则增加
             */
            AuditIpMqMessage auditIpMqMessage = new AuditIpMqMessage();
            List<String> list = Lists.newArrayList();
            list.add(String.valueOf(vo.getId()));
            auditIpMqMessage.setAuditIps(list);
            auditIpMqMessage.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            auditIpMqMessage.setType("AuditIpAdd");
            MqUtil.getInstance(MqParamsUtil.getMqHost(), MqParamsUtil.getMqUser(), MqParamsUtil.getMqPwd()).sendBroacastMsg2Exch("amq.fanout", JSON.toJSONString(auditIpMqMessage));
        }
        return result;
    }

    @Override
    public int modifyIpSetting(IpSettingVo vo){
        if(ipSettingManageMapper.countIdByNameAndId(vo.getName(),vo.getId()) > 0){
            throw new RuntimeException(vo.getName() + "已存在");
        }
        vo.setUpdateTime(new Date());
        Integer result = ipSettingManageMapper.modifyIpSetting(vo);
        if(result>0) {
            /**
             * yyangs mq消息 安全审计规则增加
             */
            AuditIpMqMessage auditIpMqMessage = new AuditIpMqMessage();
            List<String> list = Lists.newArrayList();
            list.add(String.valueOf(vo.getId()));
            auditIpMqMessage.setAuditIps(list);
            auditIpMqMessage.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            auditIpMqMessage.setType("AuditIpMod");
            MqUtil.getInstance(MqParamsUtil.getMqHost(), MqParamsUtil.getMqUser(), MqParamsUtil.getMqPwd()).sendBroacastMsg2Exch("amq.fanout", JSON.toJSONString(auditIpMqMessage));
        }
        return result;
    }

    @Override
    public PageList<IpSettingVo> findIpSetting(int pageNumber,int pageSize,String  keyword){
        PageList<IpSettingVo> list = ipSettingManageMapper.findIpSetting(new PageBounds(pageNumber, pageSize),keyword);
        int pageNum = pageNumber == 0 ?1 :pageNumber;
        int base = (pageNum - 1) * pageSize;
        int i=0;
        for(IpSettingVo vo : list){
            vo.buildIpsWithIp();
            vo.setNum(base + ++i);
        }
        return list;
    }

    @Override
    public void removeIpSettingById(String id){
        if(StringUtils.isEmpty(id)) return;
        int result = ipSettingManageMapper.removeIpSettingById(id);
        if(result>0) {
            /**
             * yyangs mq消息 安全审计规则增加
             */
            AuditIpMqMessage auditIpMqMessage = new AuditIpMqMessage();
            List<String> list = Lists.newArrayList();
            list.add(String.valueOf(id));
            auditIpMqMessage.setAuditIps(list);
            auditIpMqMessage.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            auditIpMqMessage.setType("AuditIpMod");
            MqUtil.getInstance(MqParamsUtil.getMqHost(), MqParamsUtil.getMqUser(), MqParamsUtil.getMqPwd()).sendBroacastMsg2Exch("amq.fanout", JSON.toJSONString(auditIpMqMessage));
        }
    }

    @Override
    public int batchRemoveIpSetting(List<String> ids){
        if(ids == null || ids.size() == 0) return 1;
        List<String> list = Lists.newArrayList();
        for(String id : ids){
            Integer result = ipSettingManageMapper.removeIpSettingById(id);
            if(result>0){
                list.add(String.valueOf(id));
            }
        }
        if(list.size()>0){
            /**
             * yyangs mq消息 安全审计规则增加
             */
            AuditIpMqMessage auditIpMqMessage = new AuditIpMqMessage();
            auditIpMqMessage.setAuditIps(list);
            auditIpMqMessage.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            auditIpMqMessage.setType("AuditIpMod");
            MqUtil.getInstance(MqParamsUtil.getMqHost(), MqParamsUtil.getMqUser(), MqParamsUtil.getMqPwd()).sendBroacastMsg2Exch("amq.fanout", JSON.toJSONString(auditIpMqMessage));
        }
        return 1;
    }

    @Override
    public List<IpSettingVo> findAllIpSetting( String  keyword){
        List<IpSettingVo> list = ipSettingManageMapper.findAllIpSetting(keyword);
        list.stream().forEach(vo->{vo.buildIpsWithIp();});
        return list;
    }
}
